package model;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class PostModel {
    Connection connection;
    
    public PostModel(Connection connection) {
        this.connection = connection;
    }
    
    public int insertPost(String title, String post_type, int poster_id, String content) throws SQLException {
        String query = "INSERT INTO post (title, post_type, poster_id, datetime, content ) VALUES ('"+title+"', '"+ post_type +"' , '" + poster_id + "', NOW(), '"+ content+"')" ;        
        System.out.println("query : " + query);
        Statement statement = connection.createStatement();
        String[] keyColumn = {"ID"};
        statement.executeUpdate(query, keyColumn);        
        
        ResultSet r = statement.getGeneratedKeys();
        r.next();                
        int post_id = r.getInt(1);
        return post_id;
    }
    
    public ResultSet getPost(int post_id) throws SQLException {
        String query = "SELECT *, post.comment_count AS post_comment_count, post.like_count AS post_like_count "
                + "FROM post "
                + "INNER JOIN user ON"
                + " user.ID=poster_id "
                + "WHERE post.ID="+post_id
                + " LIMIT 1";
        Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        ResultSet rs = statement.executeQuery(query);
        return rs;
    }
    
       
    public ResultSet getPostOrderedLimited(String sortby, int limited) throws SQLException {            
        String query = "SELECT * , post.ID AS post_id, post.comment_count AS post_comment_count "
                + "FROM post "
                + "INNER JOIN user "
                + "ON user.ID=poster_id "
                + "ORDER BY " + sortby + " DESC "
                + "LIMIT " + limited;
        Statement statement = connection.createStatement();
        ResultSet res = statement.executeQuery(query);        
        return res;
    }	

    public String getLikeWithClassString(int likecount,int user_id,int post_id) throws SQLException {
            String q = "SELECT * "
                    + "FROM post_who_like "
                    + "WHERE user_id='" + user_id + "'" 
                    + "AND post_id='" + post_id + "'";
            if(isPostLikedByUser(user_id,post_id))
            {
                    Statement statement = connection.createStatement();
                    ResultSet res = statement.executeQuery(q);           
                    res.next();
                    return "<small><i class=\"content-like-" + post_id + "\">You've " + res.getString("like_or_dislike") + "d this (" + likecount + " people like this)</i></small>";
            }
            else
            {
                    return "<small><i class=\"content-like-$post_id\">" + likecount + "  people like this</i></small>";
            }
    }
    
    public boolean isPostLikedByUser(int user_id,int post_id) throws SQLException
    {
            String query = "SELECT * "
                    + "FROM post_who_like "
                    + "WHERE user_id='" + user_id + "' AND post_id='" + post_id + "'";
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery(query);                                    
            //rs.last() mengembalikan true jika row di rs melebihi 0
            boolean haslike = (rs.last()) ? true:false; //false jika user belum melike post tersebut
            return haslike;
    }
    
    public void incPostCommentCount(int post_id) throws SQLException {
        String query = "UPDATE post SET comment_count=comment_count+1 WHERE ID=" + post_id;
        Statement statement = connection.createStatement();
        statement.executeUpdate(query);                
    }
    
    public void decPostCommentCount(int post_id) throws SQLException {
        String query ="UPDATE post "
                + "SET comment_count=comment_count-1 "
                + "WHERE ID=" + post_id ;                         
        Statement statement = connection.createStatement();
        statement.executeUpdate(query);
    }
}
